<?php
/**
 * Created by PhpStorm.
 * User: Ysll <754060604@qq.com>
 * Date: 2017/10/9
 * Time: 15:57
 */

namespace Admin\Controller;


use Admin\Model\ContactUs;
use Admin\Model\Introduction;
use Admin\Model\WebSetting;
use Common\Common\FileSystem;
use Common\Common\Pagination;
use Common\Common\ResponseResult;
use Common\Common\Validator;

class WebSettingController extends Controller
{
    /**
     * 管理水印页面
     */
    public function antiFake()
    {
        $this->checkPermission('anti-fake-edit');
        $image = WebSetting::findByKey(WebSetting::KEY_ANTI_FAKE_IMAGE);
        $text = WebSetting::findByKey(WebSetting::KEY_ANTI_FAKE_TEXT);
        $this->assign('image', $image);
        $this->assign('text', $text);
        $this->display('admin/set/anti_fake');
    }

    /**
     * 更新或创建图片水印
     */
    public function updateAntiFakeImage()
    {
        $this->checkPermission('anti-fake-edit');
        if (!IS_POST) {
            ResponseResult::error(1000, '请求错误');
        }
        $v = Validator::make($_FILES, [
            'file' => 'required|file:image'
        ]);
        if ($v->filed()) {
            ResponseResult::error(2000, $v->error());
        }

        $path = FileSystem::uploadFile2NoAntiFake('file');

        if (WebSetting::saveOrUpdate(WebSetting::KEY_ANTI_FAKE_IMAGE, $path)) {
            ResponseResult::success('保存成功');
        } else {
            ResponseResult::error(3000, '保存失败');
        }
    }

    /**
     * 更新或创建文字水印
     */
    public function updateAntiFakeText()
    {
        $this->checkPermission('anti-fake-edit');
        if (!IS_POST) {
            ResponseResult::error(1000, '请求错误');
        }
        $v = Validator::make($_POST, [
            'text' => 'required|max:64'
        ]);
        if ($v->filed()) {
            ResponseResult::error(2000, $v->error());
        }

        if (WebSetting::saveOrUpdate(WebSetting::KEY_ANTI_FAKE_TEXT, $_POST['text'])) {
            ResponseResult::success('保存成功');
        } else {
            ResponseResult::error(3000, '保存失败');
        }
    }

    /**
     * 更换水印状态
     */
    public function switchAntiFakeIsEnable()
    {
        $this->checkPermission('anti-fake-edit');
        if (!IS_POST) {
            ResponseResult::error(1000, '请求错误');
        }
        $v = Validator::make($_POST, [
            'key' => 'required|max:64'
        ]);
        if ($v->filed()) {
            ResponseResult::error(2000, $v->error());
        }
        if (WebSetting::switchIsEnable($_POST['key'])) {
            ResponseResult::success('更新成功');
        } else {
            ResponseResult::error(3000, '更新失败');
        }
    }

    /**
     * 联系我们
     */
    public function contactUs()
    {
        $this->checkPermission('contact-us-edit');
        $this->assign('contact', ContactUs::findOne());
        $this->display('admin/set/contact_us');
    }

    /**
     * 创建或更新联系我们
     */
    public function updateContactUs()
    {
        $this->checkPermission('contact-us-edit');
        if (!IS_POST) {
            $this->error('请求错误');
        }
        $validator = Validator::make(array_merge($_POST, $_FILES), [
            'id' => 'exists:contact_us,id',
            'name' => 'required|max:32',
            'address' => 'required|max:64',
            'tel' => 'required|max:32',
            'email' => 'required|max:64|email',
            'website' => 'required|max:255',
            'fax' => 'max:32',
            'icon' => 'file:image'
        ]);
        if ($validator->filed()) {
            $this->error('数据错误', '', false, $validator);
        }
        $data = array_only($_POST, [
            'id', 'name', 'address', 'tel', 'email', 'website', 'fax'
        ]);
        if (isset($_FILES['icon'])) {
            $data['icon'] = FileSystem::uploadFile2NoAntiFake('icon');
        }
        if (ContactUs::updateOrSave($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }

    /**
     * 留言消息列表
     */
    public function leaveWord()
    {
        $this->checkPermission('leave-word-edit');
        $validator = Validator::make($_REQUEST, [
            'page' => 'integer|between:0,9999'
        ]);
        $page = I('request.page', 1);
        $search = I('request.search', null);
        if ($validator->filed()) {
            $page = 1;
        }

        $where = null;
        if ($search != null) {
            $s = "'%$search%'";
            $where = "name like $s or qq like $s or email like $s";
        }
        $leaveWords = Pagination::make($page, 20, M('leave_word')->order('created_at desc'), $where);
        $this->assign('leaveWords', $leaveWords);
        $this->assign('data', $leaveWords->data);
        $this->assign('search', $search);
        $this->display('admin/set/leave_word');
    }

    /**
     * 删除留言
     */
    public function deleteLeaveWord()
    {
        $this->checkPermission('leave-word-edit');
        if (!IS_POST) {
            ResponseResult::error(1000, '请求错误');
        }

        $validator = Validator::make($_POST, [
            'id' => 'required|exists:leave_word,id'
        ]);
        if ($validator->filed()) {
            ResponseResult::error(2000, $validator->error());
        }

        if (M('leave_word')->where('id = ' . $_POST['id'])->delete()) {
            ResponseResult::success('删除成功');
        }
        ResponseResult::error(3000, '删除失败');
    }

    /**
     * 关于我们
     */
    public function introduction()
    {
        $this->checkPermission('introduction-edit');
        $this->assign('data', Introduction::getInstance()->order('created_at desc')->select());
        $this->display('admin/introduction');
    }

    /**
     * 创建关于我们
     */
    public function introductionAdd()
    {
        $this->checkPermission('introduction-edit');
        $this->display('admin/introduction_add');
    }

    /**
     * 编辑关于我们
     */
    public function introductionEdit()
    {
        $this->checkPermission('introduction-edit');
        $v = Validator::make($_REQUEST, ['id' => 'required|exists:introduction,id']);
        if ($v->filed()) {
            $this->error('数据不存在');
        }
        $data = Introduction::findById($_REQUEST['id']);
        $this->assign('data', $data);
        $this->display('admin/introduction_edit');
    }

    /**
     * 插入introduction数据
     */
    public function insertIntroduction()
    {
        $this->checkPermission('introduction-edit');
        if (!IS_POST) {
            $this->error('请求错误');
        }

        $validator = Validator::make($_POST, [
            'type' => 'required|max:32',
            'title' => 'required|max:64',
            'content' => 'required',
        ]);
        if ($validator->filed()) {
            $this->error('数据错误', '', false, $validator);
        }
        $data = array_only($_POST, ['type', 'title', 'content']);
        if (Introduction::getInstance()->add($data)) {
            $this->success('保存成功', '/admin/setting/introduction/list');
        } else {
            $this->error('保存失败');
        }
    }

    /**
     * 更新introduction数据
     */
    public function updateIntroduction()
    {
        $this->checkPermission('introduction-edit');
        if (!IS_POST) {
            $this->error('请求错误');
        }
        $validator = Validator::make($_POST, [
            'id' => 'required|exists:introduction,id',
            'type' => 'required|max:32',
            'title' => 'required|max:64',
            'content' => 'required',
        ]);
        if ($validator->filed()) {
            $this->error('数据错误', '', false, $validator);
        }

        if (Introduction::update($_POST['id'], array_only($_POST, ['type', 'title', 'content']))) {
            $this->success('保存成功', '/admin/setting/introduction/list');
        } else {
            $this->error('保存失败');
        }
    }

    /**
     * 删除 introduction
     */
    public function deleteIntroduction()
    {
        $this->checkPermission('introduction-edit');
        if (!IS_POST) {
            ResponseResult::error(1000, '请求错误');
        }
        $v = Validator::make($_REQUEST, ['id' => 'required|exists:introduction,id']);
        if ($v->filed()) {
            ResponseResult::error(2000, $v->error());
        }
        if (Introduction::destroy($_POST['id'])) {
            ResponseResult::success('删除成功');
        }
        ResponseResult::error(3000, '删除失败');
    }
}